/**
 * 设置超链接跳转时自动加上token值
 */
define(function (require, exports, module) {
    var environment = require('environment');
    var util = require('/static_public/js/util');

    function setUrl($a) {
        var url = $a.attr('href');
        var token = util.getToken();

        var domain = $a.attr('domain');
        if (domain && url.indexOf('http://') === -1) {
            url = environment[domain] + url;

            $a.attr('href', url);
        }

        if (token) {
            var noToken = $a.attr('data-no-token');
            // except:1、attr data-no-token 2、javascript: 3、# 4、mailto ...or more
            if ((typeof noToken === 'undefined') && url && url.indexOf('javascript') !== 0 && url.indexOf('#') !== 0 && url.indexOf('mailto') !== 0) {
                url = util.setUrlPara('token', token, url);
                $a.attr('href', url);
            }
        }

        return url;
    }

    $(document)
        .on('click', 'a', function (e) {
            setUrl($(this));
        })
        .on('contextmenu', 'a', function () {
            setUrl($(this));
        });
});